package cn.tedu.epidemic.webapi.service.impl;

import cn.tedu.community.commons.excepion.ServiceException;
import cn.tedu.community.commons.pojo.comment.dto.CommentAddNewDTO;
import cn.tedu.community.commons.pojo.comment.entity.Comment;
import cn.tedu.community.commons.pojo.comment.entity.CommentLike;
import cn.tedu.community.commons.pojo.comment.vo.CommentListItemVO;
import cn.tedu.community.commons.pojo.forum.vo.ForumStandardVO;
import cn.tedu.community.commons.restful.JsonPage;
import cn.tedu.community.commons.restful.ServiceCode;
import cn.tedu.epidemic.service.CommentService;
import cn.tedu.epidemic.service.PhotoUploadService;
import cn.tedu.epidemic.webapi.mapper.CommentMapper;
import cn.tedu.epidemic.webapi.mapper.ForumMapper;
import cn.tedu.epidemic.webapi.security.CurrentUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ForumMapper forumMapper;
    @Autowired
    private PhotoUploadService photoUploadService;

    @Override
    public void insert(CommentAddNewDTO addNewDTO) {
        ForumStandardVO standardById = forumMapper.getStandardById(addNewDTO.getForumId());
        if (standardById == null) {
            String message = "发布评论失败，该论坛已不存在！";
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,message);
        }

        if (standardById.getAuditStatus() == 0) {
            throw new ServiceException(ServiceCode.ERR_SELECT,
                    "请等待管理员审核！");
        }
        if (standardById.getAuditStatus() == 2) {
            throw new ServiceException(ServiceCode.ERR_SELECT,
                    "论坛存在敏感信息或图片，请编辑修改后等待管理员审核!");
        }

        Long userId = CurrentUser.getUserId();
        Comment comment = new Comment();
        BeanUtils.copyProperties(addNewDTO, comment);
        comment.setUserId(userId);
        int insert = commentMapper.insert(comment);
        if (insert < 1) {
            String message = "发布评论失败，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteByIdForumId(Long id, Long forumId) {
        Long userId = CurrentUser.getUserId();
        int countByIdAndUserId = forumMapper.countByIdAndUserId(forumId, userId);
        if (countByIdAndUserId < 1) {
            String message = "操作失败，您不是该论坛的发布者！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        ForumStandardVO forumStandardVO = forumMapper.getStandardById(forumId);
        if (forumStandardVO == null) {
            String message = "删除评论失败，当前论坛已不存在！";
            throw new ServiceException(ServiceCode.ERR_NOTFOUND, message);
        }

        CommentListItemVO selectById = commentMapper.selectById(id);
        if (selectById == null) {
            String message = "删除评论失败，当前评论已不存在！";
            throw new ServiceException(ServiceCode.ERR_NOTFOUND, message);
        }
        commentMapper.deleteLikeByComId(id);
        int delete = commentMapper.deleteByIdForumId(id, forumId);
        if (delete < 1) {
            String message = "删除评论失败，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        if (selectById.getPhoto()!=null){
            photoUploadService.deletePhoto(selectById.getPhoto());
        }
    }

    @Override
    public void deleteByIdAndUserId(Long comId) {
        CommentListItemVO selectById = commentMapper.selectById(comId);
        if (selectById == null) {
            String message = "删除失败，该评论不存在！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        Long userId = CurrentUser.getUserId();
        commentMapper.deleteLikeByComId(comId);
        int delete = commentMapper.deleteByIdAndUserId(comId, userId);
        if (delete < 1) {
            String message = "删除评论失败，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        if (selectById.getPhoto()!=null){
            photoUploadService.deletePhoto(selectById.getPhoto());
        }
    }

    @Override
    public JsonPage<CommentListItemVO> commentListByPage(Long forumId, Integer page, Integer pageSize) {
        ForumStandardVO forumStandardVO = forumMapper.getStandardById(forumId);
        if (forumStandardVO == null) {
            String message = "分页查询评论列表失败，该论坛不存在！";
            throw new ServiceException(ServiceCode.ERR_NOTFOUND, message);
        }
        PageHelper.startPage(page, pageSize);
        List<CommentListItemVO> commentListItemVOS = commentMapper.commentListByForumId(forumId);
        for (CommentListItemVO commentListItemVO : commentListItemVOS) {
            commentListItemVO.setCountByLike(commentMapper.countByComId(commentListItemVO.getId()));
        }
        return JsonPage.restPage(new PageInfo<>(commentListItemVOS));
    }

    @Override
    public String addLike(Long commentId) {
        CommentListItemVO selectById = commentMapper.selectById(commentId);
        if (selectById == null) {
            String message = "点赞失败，该评论已不存在！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        Long userId = CurrentUser.getUserId();
        int countComIdUserId = commentMapper.countComIdUserId(commentId, userId);
        if (countComIdUserId >= 1) {
            int deleteLike = commentMapper.deleteLike(commentId, userId);
            if (deleteLike < 1) {
                throw new ServiceException(ServiceCode.ERR_DELETE, "取消点赞失败，请稍后重试！");
            }
            return "取消点赞成功！";
        }
        CommentLike commentLike = new CommentLike();
        commentLike.setCommentId(commentId);
        commentLike.setUserId(userId);
        int addLike = commentMapper.addLike(commentLike);
        if (addLike < 1) {
            String message = "点赞失败，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        return "点赞成功！";
    }

}
